package com.systemCapacity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemCapacity.common.dto.*;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.entity.BattleCapacityResolve;
import com.systemCapacity.entity.BattlePlan;
import com.systemCapacity.entity.BattleTask;
import com.systemCapacity.entity.Enum.BattleActivityType;
import com.systemCapacity.entity.Enum.TaskType;
import com.systemCapacity.entity.Enum.TenCapacityType;
import com.systemCapacity.entity.PlanRecommend;
import com.systemCapacity.mapper.BattlePlanMapper;
import com.systemCapacity.mapper.BattleTaskMapper;
import com.systemCapacity.service.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class BattlePlanServiceImpl extends ServiceImpl<BattlePlanMapper, BattlePlan> implements BattlePlanService {

    @Autowired
    BattlePlanMapper battlePlanMapper;

    @Autowired
    BattleActivityService battleActivityService;

    @Autowired
    BattlePlatformService battlePlatformService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    EquipmentTemplateService equipmentTemplateService;

    @Autowired
    PlanRecommendService planRecommendService;

    @Autowired
    BattleTaskMapper battleTaskMapper;

    @Autowired
    BlueBattlePlatformService blueBattlePlatformService;

    @Autowired
    BattleCapacityResolveService battleCapacityResolveService;

    /**
     * 读取作战方案
     * @param taskId 作战任务id
     * @return 作战方案列表
     */
    @Override
    public List<BattlePlanDto> getBattlePlans(String taskId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taskId", taskId);
        List<BattlePlan> plans = battlePlanMapper.selectList(queryWrapper);
        List<BattlePlanDto> dtos = plans.stream().map(
                battlePlan -> BeanUtil.copyProperties(battlePlan, BattlePlanDto.class)
        ).collect(Collectors.toList());

        for (BattlePlanDto dto : dtos) {
            dto.setActivities(battleActivityService.getActivities(dto.getPlanId(), true));
            getPlanEvaluateDes(dto);
        }
        return dtos;
    }

    /**
     * 读取作战方案
     * @param taskId 作战任务id
     * @return 作战方案列表
     */
    @Override
    public List<BattlePlanDto> getBattlePlansNew(String taskId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taskId", taskId);
        List<BattlePlan> plans = battlePlanMapper.selectList(queryWrapper);
        List<BattlePlanDto> dtos = plans.stream().map(
                battlePlan -> BeanUtil.copyProperties(battlePlan, BattlePlanDto.class)
        ).collect(Collectors.toList());

        //查询蓝方平台
        List<BlueBattlePlatformDto> blueBattlePlatformDtoList = blueBattlePlatformService.queryPlatformListByTaskId(taskId);

        StringBuilder sb = new StringBuilder();
        //蓝军平台描述
        if (!blueBattlePlatformDtoList.isEmpty()) {
            Map<String, List<BlueBattlePlatformDto>> groupByEquip = blueBattlePlatformDtoList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
            groupByEquip.forEach((k,v) -> {
                sb.append(k + " x " + v.size() + "\n");
            });
        }

        for (BattlePlanDto dto : dtos) {
            dto.setBluePlatFormList(blueBattlePlatformDtoList);
            dto.setBluePlatFormDesc(sb.toString());
            //查询红方活动
            dto.setActivities(battleActivityService.getActivitiesByPlanId(dto.getPlanId()));
            getEvaluatePlanInfo(dto);
        }
        return dtos;
    }

    /**
     * 读取推荐方案
     * @return 推荐方案列表
     */
    @Override
    public List<BattlePlanDto> getRecommendPlans() {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("taskId");
        List<BattlePlan> plans = battlePlanMapper.selectList(queryWrapper);
        List<BattlePlanDto> dtos = plans.stream().map(
                battlePlan -> BeanUtil.copyProperties(battlePlan, BattlePlanDto.class)
        ).collect(Collectors.toList());

        for (BattlePlanDto dto : dtos) {
            //查询蓝方平台
            List<BlueBattlePlatformDto> blueBattlePlatformDtoList = blueBattlePlatformService.queryPlatformListByTaskId("");
            dto.setBluePlatFormList(blueBattlePlatformDtoList);
            //蓝军平台描述
            if (!blueBattlePlatformDtoList.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                Map<String, List<BlueBattlePlatformDto>> groupByEquip = blueBattlePlatformDtoList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
                groupByEquip.forEach((k,v) -> {
                    sb.append(k + " x " + v.size() + "\n");
                });
                dto.setBluePlatFormDesc(sb.toString());
            }
        }
        return dtos;
    }

    /**
     * 查询作战方案详情
     * @param planId 方案id
     * @return 作战方案
     */
    @Override
    public BattlePlanDto getBattlePlansDetail(String planId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", planId);
        BattlePlanDto dto = BeanUtil.copyProperties(getOne(queryWrapper), BattlePlanDto.class);
        //查询蓝方平台
        List<BlueBattlePlatformDto> blueBattlePlatformDtoList = blueBattlePlatformService.queryPlatformListByTaskId(dto.getTaskId());
        dto.setBluePlatFormList(blueBattlePlatformDtoList);
        //蓝军平台描述
        if (!blueBattlePlatformDtoList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            Map<String, List<BlueBattlePlatformDto>> groupByEquip = blueBattlePlatformDtoList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
            groupByEquip.forEach((k,v) -> {
                sb.append(k + " x " + v.size() + "\n");
            });
            dto.setBluePlatFormDesc(sb.toString());

        }
        //查询红方活动
        dto.setActivities(battleActivityService.getActivitiesByPlanId(dto.getPlanId()));
        return dto;
    }

    /**
     * 查询推荐方案详情
     * @param recommendPlanId 方案id
     * @return 作战方案
     */
    @Override
    public BattlePlanDto getRecommendPlan(String recommendPlanId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", recommendPlanId);
        BattlePlanDto dto = BeanUtil.copyProperties(getOne(queryWrapper), BattlePlanDto.class);
        //查询蓝方平台
        List<BlueBattlePlatformDto> battlePlatformDtoList = blueBattlePlatformService.queryPlatformListByTaskId(recommendPlanId);
        dto.setBluePlatFormList(battlePlatformDtoList);
        //蓝军平台描述
        if (!battlePlatformDtoList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            Map<String, List<BlueBattlePlatformDto>> groupByEquip = battlePlatformDtoList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
            groupByEquip.forEach((k,v) -> {
                sb.append(k + " x " + v.size() + "\n");
            });
            dto.setBluePlatFormDesc(sb.toString());
            //根据蓝军平台查询推荐表，获取推荐的方案id
            QueryWrapper<PlanRecommend> recommendQueryWrapper = new QueryWrapper<>();
            String bluePlatform = dto.getBluePlatFormDesc().replaceAll("\n","");
            recommendQueryWrapper.eq("bluePlatform",bluePlatform);
            List<PlanRecommend> recommendList = planRecommendService.list(recommendQueryWrapper);
            if (!recommendList.isEmpty()) {
                PlanRecommend planRecommend = recommendList.get(0);
                String planId = planRecommend.getPlanId();
                //查询红方活动
                dto.setActivities(battleActivityService.getActivitiesByPlanId(planId));
            }
        }
        return dto;
    }

    /**
     * 推荐方案
     * @param taskId 任务id
     * @return 生成作战方案
     */
    @Override
    public Result saveRecommendPlan(String taskId) {
        BattlePlanDto dto = new BattlePlanDto();
        dto.setTaskId(taskId);
        dto.setPlanName("推荐方案");
        //查询蓝方平台
        List<BlueBattlePlatformDto> battlePlatformDtoList = blueBattlePlatformService.queryPlatformListByTaskId(taskId);
        if(battlePlatformDtoList.isEmpty()){
            return Result.fail("蓝方平台不存在");
        }
        dto.setBluePlatFormList(battlePlatformDtoList);

        //计算蓝方权重
        double blueEquipWeight = 0.0;
        double blueDeviceWeight = 0.0;
        for (BlueBattlePlatformDto blueBattlePlatformDto : battlePlatformDtoList) {
            blueEquipWeight += blueBattlePlatformDto.getEquipmentWeight();
            for (BattlePlatformDeviceDto battlePlatformDeviceDto : blueBattlePlatformDto.getPlatformDeviceList()) {
                blueDeviceWeight += battlePlatformDeviceDto.getDeviceWeight();
            }
        }

        //蓝军平台方案推荐
        Map<String, List<BlueBattlePlatformDto>> groupByEquip = battlePlatformDtoList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
        List<String> equipList = new ArrayList<>();
        List<String> numList = new ArrayList<>();
        groupByEquip.forEach((k,v) -> {
            equipList.add(k);
            numList.add(v.size() + "");
        });
        //根据蓝军平台查询推荐表，获取推荐的方案id
        QueryWrapper<PlanRecommend> recommendQueryWrapper = new QueryWrapper<>();
        String bluePlatform = equipList.stream().collect(Collectors.joining(";"));
        String equipmentNum = numList.stream().collect(Collectors.joining(";"));

        recommendQueryWrapper.eq("bluePlatform",bluePlatform)
                .eq("equipmentNum",equipmentNum);

        List<PlanRecommend> recommendList = planRecommendService.list(recommendQueryWrapper);
        if(recommendList.isEmpty()){
            return Result.fail("没有可推荐的方案");
        }
        PlanRecommend planRecommend = recommendList.get(0);
        String planId = planRecommend.getPlanId();
        if(!planExists(planId)){
            return Result.fail("方案不存在");
        }
        //查询红方活动
        dto.setActivities(battleActivityService.getActivitiesByPlanId(planId));
        //计算红方权重
        double redEquipWeight = 0.0;
        double redDeviceWeight = 0.0;
        List<BattleActivityDto> activities = dto.getActivities();
        for (BattleActivityDto activity : activities) {
            List<BattlePlatformDto> platFormList = activity.getPlatFormList();
            if(platFormList.isEmpty()){
                continue;
            }
            for (BattlePlatformDto battlePlatformDto : platFormList) {
                redEquipWeight += battlePlatformDto.getEquipmentWeight();
                for (BattlePlatformDeviceDto battlePlatformDeviceDto : battlePlatformDto.getPlatformDeviceList()) {
                    redDeviceWeight += battlePlatformDeviceDto.getDeviceWeight();
                }
            }
        }
        
        //判断红蓝方权重
        if((redEquipWeight + redDeviceWeight) < (blueEquipWeight + blueDeviceWeight)){
            return Result.succ("红方权重小于蓝方");
        }
        saveBattlePlanNew(dto,false, true);
        return Result.succ("推荐成功");
    }

    private void getPlanEvaluateDes(BattlePlanDto dto) {
        StringBuilder stringBuilder = new StringBuilder();
        double totalPerformanceNum = 0.0;
        double satisfiedPerformanceNum = 0.0;
        for (BattleActivityDto activityDto : dto.getActivities()) {
            battleActivityService.evaluateActivity(activityDto);
            if (!activityDto.getSatisfiedStr().isEmpty()) {
                if (stringBuilder.length() == 0) {
                    stringBuilder.append(String.format("方案%s不满足要求:\n", dto.getPlanName()));
                }
                stringBuilder.append(String.format("作战环节%s不满足要求,", activityDto.getActivityType().getDes()));
                stringBuilder.append(activityDto.getSatisfiedStr());
                stringBuilder.append("\n");
            }
            for (ActivityResourceRequestDto requestDto : activityDto.getActivityResourceRequests()) {
                for (ActivityPerformanceRequestDto performanceRequestDto : requestDto.getPerformanceRequests()) {
                    totalPerformanceNum++;
                    if (performanceRequestDto.isSatisfied()) {
                        satisfiedPerformanceNum++;
                    }
                }
            }
        }
        if (satisfiedPerformanceNum < totalPerformanceNum * 0.7) {
            stringBuilder.append("作战方案性能参数满足度小于70%");
        }
        dto.setPlanDes(stringBuilder.toString());
    }

    private void getEvaluatePlanInfo(BattlePlanDto dto){
        String taskId = dto.getTaskId();
        //查询任务
        LambdaQueryWrapper<BattleTask> taskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        taskLambdaQueryWrapper.eq(BattleTask::getTaskId,taskId);
        BattleTask task = battleTaskMapper.selectOne(taskLambdaQueryWrapper);
        TaskType taskType = convertTaskType(task.getTaskType());
        //查询任务能力分解
        List<BattleCapacityResolveDto> battleCapacityResolveList = battleCapacityResolveService.getResolveCapacityList(taskId);
        BattleTaskDto taskDto = new BattleTaskDto();
        BeanUtils.copyProperties(task,taskDto);
        taskDto.setCapacityResolveList(battleCapacityResolveList);
        taskDto.setTaskType(taskType);
        //步骤评估
        evaluateActivity(dto.getActivities(),taskDto);

        //方案评估
        List<BattleActivityDto> activityDtoList = dto.getActivities();
        String activitySteps = activityDtoList.stream().map(x -> x.getActivityType().getDes()).collect(Collectors.joining());

        boolean planFlg = true;
        switch (taskType){
            case RECONNOITRE :
                //发现 跟踪 定位
                if(!activitySteps.equals("发现跟踪定位")){
                    planFlg = false;
                }
                break;
            case INTERFERENCE:
                //发现 跟踪 定位 干扰
                if(!activitySteps.equals("发现跟踪定位干扰")){
                    planFlg = false;
                }
                break;
            case STRIKING:
                //发现 跟踪 定位 打击 评估
                if(!activitySteps.equals("发现跟踪定位打击评估")){
                    planFlg = false;
                }
                break;
            case DISCOVERING:
                break;
            case TRACKING:
                break;
            case POSITIONING:
                break;
            case ESTIMATING:
                break;
        }
        dto.setPlanSatisfied(planFlg);
    }

    private TaskType convertTaskType(Integer type){
        TaskType taskType = null;
        switch (type){
            case 0 :
                taskType = TaskType.RECONNOITRE;
                break;
            case 1:
                taskType = TaskType.INTERFERENCE;
                break;
            case 2:
                taskType = TaskType.STRIKING;
                break;
            case 3:
                taskType = TaskType.DISCOVERING;
                break;
            case 4:
                taskType = TaskType.TRACKING;
                break;
            case 5:
                taskType = TaskType.POSITIONING;
                break;
            case 6:
                taskType = TaskType.ESTIMATING;
                break;
        }
        return taskType;
    }

    private void evaluateActivity(List<BattleActivityDto> activityList, BattleTaskDto task) {

//        List<BattleCapacityResolveDto> capacityResolveDtoList = task.getCapacityResolveList();
//        List<String> capacityResoveNameList = new ArrayList<>();
//        capacityResolveDtoList.forEach(item -> {
//            if(item.getChildCapacities() != null && !item.getChildCapacities().isEmpty()){
//                for (BattleCapacityResolveDto childCapacity : item.getChildCapacities()) {
//                    capacityResoveNameList.add(childCapacity.getCapacityName());
//                }
//            }
//        });
        for (BattleActivityDto activity : activityList) {
            //获取平台
            List<BattlePlatformDto> platformList = activity.getPlatFormList();
            List<String> templateNameList = platformList.stream().map(BattlePlatformDto::getTemplateName).collect(Collectors.toList());
            boolean flag = true;
            for (BattlePlatformDto battlePlatformDto : platformList) {
                List<String> capacityNameList = battlePlatformDto.getCapacityDtoList().stream().map(c -> c.getCapacityName()).collect(Collectors.toList());
                //判断能力是否满足
                boolean isMatch = battlePlatformDto.getPlatformDeviceList().stream().allMatch(x -> capacityNameList.contains(x.getCapacityName()));
                if(!isMatch){
                    flag = false;
                    break;
                }
            }
            if(!flag){
                activity.setSatisfied(false);
                continue;
            }

            TaskType taskType = task.getTaskType();
            BattleActivityType activityType = activity.getActivityType();
            StringBuilder satisfiedBuilder = new StringBuilder();
            switch (activityType){
                case DISCOVERY:
                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
                        flag = false;
                    }
                    break;
                case POSITIONING:
                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
                        flag = false;
                    }
                    break;
                case TRACKING:
                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
                        flag = false;
                    }
                    break;
                case AIMING:
                    flag = true;
                    break;
                case STRIKING:
                    if (!templateNameList.contains("歼击机") && !templateNameList.contains("无人机") && !templateNameList.contains("直升机") && !templateNameList.contains("火箭军部队")) {
                        flag = false;
                    }
                    break;
                case ESTIMATING:
                    if (!templateNameList.contains("地面固定指挥所") && !templateNameList.contains("机动指挥所")) {
                        flag = false;
                    }
                    break;
            }
//            switch (taskType){
//                case RECONNOITRE :
//                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
//                        flag = false;
//                    }
//                    break;
//                case INTERFERENCE:
//                    if (!templateNameList.contains("干扰机")) {
//                        flag = false;
//                    }
//                    break;
//                case STRIKING:
//                    if (!templateNameList.contains("歼击机") && !templateNameList.contains("无人机") && !templateNameList.contains("直升机") && !templateNameList.contains("火箭军部队")) {
//                        flag = false;
//                    }
//                    break;
//                case DISCOVERING:
//                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
//                        flag = false;
//                    }
//                    break;
//                case TRACKING:
//                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
//                        flag = false;
//                    }
//                    break;
//                case POSITIONING:
//                    if (!templateNameList.contains("侦察机") && !templateNameList.contains("预警机") && !templateNameList.contains("无人机")) {
//                        flag = false;
//                    }
//                    break;
//                case ESTIMATING:
//                    if (!templateNameList.contains("地面固定指挥所") && !templateNameList.contains("机动指挥所")) {
//                        flag = false;
//                    }
//                    break;
//            }
            activity.setSatisfied(flag);
            activity.setSatisfiedStr(satisfiedBuilder.toString());
        }
    }


    /**
     * 保存作战方案
     * @param dto 作战方案
     * @return 操作结果
     */
    @Override
    @Transactional
    public boolean saveBattlePlan(BattlePlanDto dto) {
        BattlePlan plan = new BattlePlan();
        plan.setPlanId(UUID.randomUUID().toString());
        plan.setPlanName(dto.getPlanName());
        plan.setTaskId(dto.getTaskId());
        save(plan);
        List<BattleActivityDto> activities = dto.getActivities();
        // 保存作战活动
        activities.forEach(activityDto -> battleActivityService.saveActivity(activityDto, plan.getPlanId()));
        return true;
    }

    /**
     * 保存作战方案
     * @param dto 作战方案
     * @return 操作结果
     */
    @Override
    @Transactional
    public boolean saveBattlePlanNew(BattlePlanDto dto, boolean isRemainPlanId, boolean isRecommend) {
        BattlePlan plan = new BattlePlan();
        if(isRemainPlanId){
            plan.setPlanId(dto.getPlanId());
        }else{
            plan.setPlanId(UUID.randomUUID().toString());
        }
        plan.setPlanName(dto.getPlanName());
        plan.setTaskId(dto.getTaskId());
        //保存方案
        save(plan);
//        //保存蓝方平台
        List<BlueBattlePlatformDto> bluePlatFormList = dto.getBluePlatFormList();
//        bluePlatFormList.forEach(x -> x.setTaskId(dto.getTaskId()));
//        blueBattlePlatformService.savePlatForm(bluePlatFormList);

        //红方活动
        List<BattleActivityDto> activities = dto.getActivities();
        //保存作战活动
        activities.forEach(activityDto -> battleActivityService.saveActivity(activityDto, plan.getPlanId()));

        //保存方案推荐  如果是方案推荐调用的此方法，则无需再次保存推荐方案
        if (!bluePlatFormList.isEmpty() && !isRecommend) {
            bluePlatFormList.forEach(x -> x.setEquipmentName(equipmentService.getById(x.getEquipmentId()).getEquipmentName()));
            Map<String, List<BlueBattlePlatformDto>> groupByEquip = bluePlatFormList.stream().collect(Collectors.groupingBy(x -> x.getEquipmentName()));
            List<String> equipList = new ArrayList<>();
            List<String> numList = new ArrayList<>();
            groupByEquip.forEach((k,v) -> {
                equipList.add(k);
                numList.add(v.size() + "");
            });

            if (!equipList.isEmpty()) {
                PlanRecommend planRecommend = new PlanRecommend();
                planRecommend.setId(UUID.randomUUID().toString());
                planRecommend.setBluePlatform(equipList.stream().collect(Collectors.joining(";")));
                planRecommend.setEquipmentNum(numList.stream().collect(Collectors.joining(";")));
                planRecommend.setPlanId(plan.getPlanId());
                UpdateWrapper<PlanRecommend> planRecommendUpdateWrapper = new UpdateWrapper<>();
                planRecommendUpdateWrapper.eq("planId",plan.getPlanId());
                planRecommendService.saveOrUpdate(planRecommend,planRecommendUpdateWrapper);
            }
        }

        return true;
    }

    /**
     * 保存推荐方案
     * @param dto 推荐方案
     * @return 操作结果
     */
    @Override
    @Transactional
    public boolean saveRecommendPlan(BattlePlanDto dto) {
        BattlePlan plan = new BattlePlan();
        plan.setPlanId(UUID.randomUUID().toString());
        plan.setPlanName(dto.getPlanName());
        plan.setTaskId(dto.getTaskId());
        //保存方案
        save(plan);
        //保存蓝方平台
        List<BlueBattlePlatformDto> bluePlatFormList = dto.getBluePlatFormList();
        bluePlatFormList.forEach(x -> x.setTaskId(dto.getTaskId()));
        blueBattlePlatformService.savePlatForm(bluePlatFormList);
        return true;
    }
    

    /**
     * 判断作战方案是否存在
     * @param planId 作战方案id
     * @return 是否存在
     */
    @Override
    public boolean planExists(String planId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", planId);
        return getOne(queryWrapper) != null;
    }

    /**
     * 删除作战方案
     * @param planId 作战方案id
     * @return 操作结果
     */
    @Override
    @Transactional
    public boolean deletePlan(String planId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", planId);
        battleActivityService.deleteActivityByPlanId(planId);
        return removeById(planId);
    }
    /**
     * 编辑作战方案
     */
    @Override
    @Transactional
    public void editPlan(BattlePlanDto dto){
        deletePlanNew(dto.getPlanId());
        saveBattlePlanNew(dto,true, false);
    }
    /**
     * 删除作战方案
     * @param planId 作战方案id
     * @return 操作结果
     */
    @Override
    @Transactional
    public boolean deletePlanNew(String planId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", planId);

//        //删除蓝方平台
//        battlePlatformService.deletePlatformByPlanId(planId);

        //删除红方活动
        battleActivityService.deleteActivityByPlanIdNew(planId);

        //删除方案推荐
        QueryWrapper<PlanRecommend> planRecommendWrapper = new QueryWrapper<>();
        planRecommendWrapper.eq("planId",planId);
        planRecommendService.remove(planRecommendWrapper);
        return removeById(planId);
    }
    /**
     * 获取指定作战任务下的所有作战方案
     * @param taskId 作战任务
     * @return 作战方案列表
     */
    @Override
    public List<BattlePlan> getPlansByTaskId(String taskId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("taskId",taskId);
        return battlePlanMapper.selectList(queryWrapper);
    }

    /**
     * 获取作战方案
     * @param planId 作战方案id
     * @return 作战方案
     */
    @Override
    public BattlePlanDto getBattlePlan(String planId) {
        QueryWrapper<BattlePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("planId", planId);
        BattlePlanDto dto = BeanUtil.copyProperties(getOne(queryWrapper), BattlePlanDto.class);
        dto.setActivities(battleActivityService.getActivities(planId, false));
        return dto;
    }

    private boolean battleActivityResourceSatisfied(BattleActivityDto dto) {
        for (ActivityResourceRequestDto requestDto : dto.getActivityResourceRequests()) {
            if (requestDto.getSatisfiedRequestsNum() < requestDto.getPerformanceRequests().size()) {
                return false;
            }
        }
        return true;
    }
}
